
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *
 * @author antonio
 */
public class BottomUpLearn {
    
    public static CharSequence discretize(int img[][], int level)
	{
		final int len = img.length;
		String ds = "";
		int l;
		for(int i=0; i < len; i++)
		{
			for(int j = 0; j < len; j++)
			{
				l = -1;
				for(int k = 0; k <= img[i][j] ; k += 256/level)
				{	
					l = l+1;//System.out.println(l);
				}
				//System.out.println("out: " + l);
				ds += (new Integer(l)).toString();	
			}
		}
		//System.out.println(ds);
		return ds;
	}
    
    public static void main(String[] args) 
        {
            try{
                MnistManager mnist = new MnistManager("/home/antonio/Downloads/train-images.idx3-ubyte","/home/antonio/Downloads/train-labels.idx1-ubyte");
		//MnistManager mnistTest = new MnistManager("/home/antonio/Downloads/t10k-images.idx3-ubyte","/home/antonio/Downloads/t10k-labels.idx1-ubyte");
		final String DATE_FORMAT_NOW = "yyyy-MM-dd--HH:mm:ss";
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		//File fa = new File("/home/antonio/outputHierarchies/out" + sdf.format(cal.getTime()) + ".txt");
		//FileWriter fstream = new FileWriter(fa);
		//BufferedWriter fout = new BufferedWriter(fstream);
		int branches = 5;
                int minpattern = 5;
		int image[][];
		int trials = 10;
		int label[] = new int[trials];
                CharSequence bw[] = new CharSequence[trials];
		for(int i = 1; i <= trials; i++)
		{
                    //mnist.setCurrent(i%11 + 1);
                    mnist.setCurrent(i);
                    image = mnist.readImage();
                    label[i-1] = mnist.readLabel();
                    bw[i-1] = new String();
                    //if(label[i-1] == 0 || label[i-1] == 1)
                    //mnist.writeImageToPpm(image, "/home/antonio/mnist/" + i + "-" + label[i-1] + ".ppm");
                    //System.out.println(i);
                    //CharSequence bw = binarize(image,80);
                    bw[i-1] = discretize(image,4);
                    //System.out.println(bw);
                    //hrc.feed(bw);
                    //hrc.evolve();
                    //System.out.println(label[i-1] + " - " + hrc.response());		
                }
                BottomUp hrc = new BottomUp(bw,branches,minpattern,trials);
		
		//fout.close();
		}catch(Exception e)
		{
                    e.printStackTrace();
		}
	}
}
